
import React from "react";
import { useLocation, useNavigate } from "react-router-dom";

/** 筛选数据管理 */
export function useSearch(id: string) {
    const navigate = useNavigate();
    const location = useLocation();
    
    const searchData = React.useMemo(() => {
        return location.state?.[id] || {};
    }, [location.state, id]);

    const setSearchData = React.useCallback((data: any) => {
        navigate({
            pathname: location.pathname,
            search: location.search
        }, {
            state: { ...location.state, [id]: data }
        });
    }, [id, location.pathname, location.search, location.state, navigate]);

    return {
        searchData,
        setSearchData,
    }
}